﻿using System;
using System.Web.Mvc;
using System.Web.Security;
using SklepISI.Core.Interfaces;
using SklepISI.Web.Models;
using SklepISI.Web.Providers;

namespace SklepISI.Web.Controllers
{
    public class AccountController : Controller
    {
        private IUserService _userService;

        public AccountController(IUserService userService)
        {
            _userService = userService;
        }

        //
        // GET: /Account/LogOn
        
        public ActionResult LogOn()
        {
            return View();
        }

        //
        // POST: /Account/LogOn
        [HttpPost]
        public ActionResult LogOn(LogOnModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                if (Membership.ValidateUser(model.Username, model.Password))
                {

                    FormsAuthentication.SetAuthCookie(model.Username, model.RememberMe);
                   
                    if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                        && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        return RedirectToAction("Index", "Home");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "");
                }
            }

           
            //return View(model);
            return View();
        }

        //
        // GET: /Account/LogOff

        public ActionResult LogOff()
        {
            FormsAuthentication.SignOut();

            return RedirectToAction("Index", "Home");
        }

        //
        // GET: /Account/Register

        public ActionResult Register()
        {
            return View();
        }

        //
        // POST: /Account/Register

        [HttpPost]
        public ActionResult Register(AccountViewModel model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                MembershipCreateStatus createStatus;
                model.RoleId = 2;
                //Membership.CreateUser(model.User.Username, model.User.Password, model.User.Email, model.User.FirstName, model.User.LastName,true, null, out createStatus);
                createStatus = ((ISIMembershipProvider)Membership.Provider).CreateUser(model, false, null);

                if (createStatus == MembershipCreateStatus.Success)
                {
                    FormsAuthentication.SetAuthCookie(model.Username, false);
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    ModelState.AddModelError("Error", ErrorCodeToString(createStatus));
                }
            }
            return View();
           // return View(model);
        }

        //public ActionResult Delete()
        //{
        //    return View();
        //}

        public ActionResult Delete()
        {
            if (ModelState.IsValid)
            {
               Membership.DeleteUser(User.Identity.Name);
            }
            return RedirectToAction("LogOff");

        }
        //
        // GET: /Account/ChangePassword

       [Authorize]
        public ActionResult ChangePassword()
        {
            return View();
        }

        //
        // POST: /Account/ChangePassword

        
        [HttpPost]
        [Authorize]
        public ActionResult ChangePassword(ChangePasswordModel model)
        {
            if (ModelState.IsValid)
            {               
                bool changePasswordSucceeded;
                try
                {                    
                    changePasswordSucceeded = ((ISIMembershipProvider)Membership.Provider).ChangePassword(User.Identity.Name, model.OldPassword, model.Password);
                    
                }
                catch (Exception)
                {
                    changePasswordSucceeded = false;
                }

                if (changePasswordSucceeded)
                {
                    return RedirectToAction("ChangePasswordSuccess");
                }
                else
                {
                    ModelState.AddModelError("", "Nie udało się zmienić hasła");
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }

        //
        // GET: /Account/ChangePasswordSuccess

        public ActionResult ChangePasswordSuccess()
        {
            return View();
        }
        public ActionResult Properties()
        {
            return View();
        }

        #region Status Codes
        private static string ErrorCodeToString(MembershipCreateStatus createStatus)
        {
            // See http://go.microsoft.com/fwlink/?LinkID=177550 for
            // a full list of status codes.
            switch (createStatus)
            {
                case MembershipCreateStatus.DuplicateUserName:
                    return "Nazwa użytkownika już istnieje.";

                case MembershipCreateStatus.DuplicateEmail:
                    return "Wprowadzony email już istnieje.";

                case MembershipCreateStatus.InvalidPassword:
                    return "Wprowadzone hasło jest nieprawidłowe.";

                case MembershipCreateStatus.InvalidEmail:
                    return "Nieprawidłowy email.";              

                case MembershipCreateStatus.InvalidUserName:
                    return "Wprowadzona nazwa użytkownika jest nieprawidłowa.";

                case MembershipCreateStatus.ProviderError:
                    return "Błąd providera uwierzytelniania.";

                case MembershipCreateStatus.UserRejected:
                    return "Tworzenie użytkownika zostało anulowane";

                default:
                    return "Nieznany błąd.";
            }
        }
        #endregion
    }
   
}
